package com.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class BeanUtils extends org.springframework.beans.BeanUtils {

    public static <T> T propertiesCopy(Object source, Class<T> clazz) {
        if (null == source) {
            return null;
        }
        try {
            T obj = clazz.newInstance();
            org.springframework.beans.BeanUtils.copyProperties(source,obj);
            return obj;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> List<T> collectionCopy(Collection source, Class<T> clazz) {
        if (null == source) {
            return new ArrayList<>();
        }
        List<T> list = new ArrayList<>();
        for (Object o : source) {
            list.add(propertiesCopy(o, clazz));
        }
        return list;
    }
}
